package my.test.hencodertest.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import my.test.hencodertest.R;

/**
 * Created by 丁旭 on 2017/12/15.
 */

public class PaperFolding2 extends View {
    RectF rect;
    Bitmap bitmap;
    int degree = 0;
    float foldingSize = 50;

    public PaperFolding2(Context context) {
        super(context);
        init();
    }

    public PaperFolding2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PaperFolding2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.email);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        float x = (getWidth() - bitmap.getWidth()) / 2;
        float y = (getHeight() - bitmap.getHeight()) / 2;
        rect = new RectF(x, y, x + bitmap.getWidth(), y + bitmap.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Matrix matrix = new Matrix();

        float diameter = (float) Math.sqrt(getWidth() * getWidth() + getHeight() * getHeight());

        RectF normalSide = new RectF();
        normalSide.left = (getWidth() - diameter) / 2;
        normalSide.top = (getHeight() - diameter) / 2;
        normalSide.right = normalSide.left + diameter;
        normalSide.bottom = getHeight() / 2;

        RectF stretchSide = new RectF();
        stretchSide.left = (getWidth() - diameter) / 2;
        stretchSide.top = getHeight() / 2;
        stretchSide.right = stretchSide.left + diameter;
        stretchSide.bottom = stretchSide.top + diameter / 2;

        float[] pointSrc = {stretchSide.left, stretchSide.top, stretchSide.right, stretchSide.top, stretchSide.left, stretchSide.bottom, stretchSide.right, stretchSide.bottom};
        float[] pointDest = {stretchSide.left, stretchSide.top, stretchSide.right - foldingSize, stretchSide.top - foldingSize, stretchSide.left, stretchSide.bottom, stretchSide.right - foldingSize, stretchSide.bottom + foldingSize};


        canvas.save();
        canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
        canvas.clipRect(normalSide);
        canvas.rotate(-degree, getWidth() / 2, getHeight() / 2);
        canvas.drawBitmap(bitmap, (getWidth() - bitmap.getWidth()) / 2, (getHeight() - bitmap.getHeight()) / 2, null);
        canvas.restore();

        matrix.reset();
        matrix.setPolyToPoly(pointSrc, 0, pointDest, 0, 4);

        canvas.save();
        canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
        canvas.clipRect(stretchSide);
        canvas.rotate(-degree, getWidth() / 2, getHeight() / 2);
        //canvas.concat(matrix);
        canvas.drawBitmap(bitmap, (getWidth() - bitmap.getWidth()) / 2, (getHeight() - bitmap.getHeight()) / 2, null);
        canvas.restore();
    }


}
